package rd.android.hardcore.engine 
{
	import caurina.transitions.Tweener;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageOrientation;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	import fr.digitas.flowearth.conf.Conf;
	import rd.android.hardcore.event.PadsEvent;
	import rd.android.hardcore.ui.pad.Pad;
	import rd.android.hardcore.params.GlobalParams;
	import rd.android.hardcore.ui.pad.PadMask;
	import rd.android.hardcore.ui.PadsFeaturePanel;
	
	/**
	 * Définit la grille de pads (horizontale && vertical);
	 * @author Fazermokeur
	 */
	public class PadsManager extends Sprite
	{
		
		private var a_Pads	: Vector.<Pad>;
		private var a_Pages	: Vector.<PageManager>;
		
		private var _placePages:int;
		private var initPadX:int = 0;
		private var initMouseX:int;
		private var temoinX:int;
		private var scrollTimer:Timer;
		
		private var enableScroll:Boolean = false;
		private var panelChoice:PadsFeaturePanel;
		
		private var numPage:int = 0;
		private var totalPage:int = 0;
		
		private var blackSprite:Sprite;
		private var _decay:int;
		private var moving:Boolean = false;
		
		public function PadsManager() 
		{
			a_Pads	= new Vector.<Pad>();
			a_Pages	= new Vector.<PageManager>();
		
			if ( stage )	init();
			else 			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void
		{
			scrollRect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
			
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			panelChoice = new PadsFeaturePanel();
			
			var pad:Pad;
			var tracks:XML;
			
			for each(tracks in Conf.getDatas("tracks").track)
			{
				pad = new Pad(tracks);
				a_Pads.push(pad);
			}
			
			var len:int = (a_Pads.length - (a_Pads.length % 24))/24 +1;
			var page:PageManager;
			for (var i:int = 0; i < len; i++)
			{
				page = new PageManager(a_Pads.slice(i * 24, (i + 1) * 24));
				a_Pages.push(page);
			}
			
			//(a_Pages[0] as PageManager).addEventListener(PadsEvent.PAD_IS_RELEASED, showChoicePanel);
			addChild(a_Pages[0]);
			totalPage = a_Pages.length;
			
			handleResize( null );
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, handlePanelDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, handlePanelUp);
		}
		
		private function showChoicePanel(e:PadsEvent):void 
		{
			trace("addChild ChoicePanel");
			addChild(constructBlackSprite());
			addChild(panelChoice);
			panelChoice.showPanel();
		}
		
		private function constructBlackSprite():Sprite
		{
			blackSprite = new Sprite();
			blackSprite.graphics.beginFill(0x313131, .5);
			blackSprite.graphics.drawRect(0, 0, GlobalParams.WIDTH, GlobalParams.HEIGHT);
			blackSprite.graphics.endFill();
			
			return blackSprite;
		}
		
		private function handlePanelDown(e:MouseEvent):void 
		{
			//addEventListener(Event.ENTER_FRAME, moveAllPads);
			moving = true;
			initMouseX = mouseX;
		}
		
		private function moveAllPads(e:Event):void 
		{
			var destX:int = - (initMouseX - mouseX);
			Tweener.addTween(this, { placePage: destX, time:.35} );
		}
		
		private function handlePanelUp(e:MouseEvent):void 
		{
			trace("3: HANDLE PANEL UP");
			removeEventListener(Event.ENTER_FRAME, moveAllPads);
			
			checkPage();
		}
		
		/*
		 * FeedBack des pages
		 */
		private function checkPage():void
		{
			moving = false;
			if ((a_Pages[numPage] as PageManager).x < -240 && numPage < totalPage) {
				trace("4: L E F T");
				Tweener.addTween(this, {placePage:-480, time:2, onComplete:reOrder } );
			}else if ((a_Pages[numPage] as PageManager).x > 240 && numPage > totalPage) {
				trace("4: R I G H T");
				Tweener.addTween(this, {placePage:480, time:2, onComplete:reOrder} );
			}else {
				trace("4: C E N T E R");
				Tweener.addTween(this, {placePage:0, time:1 } );
			}
		}
		
		private function reOrder():void
		{
			if (!moving) {
				if ((a_Pages[numPage] as PageManager).x < -20)		numPage++;
				else if((a_Pages[numPage] as PageManager).x > 20)	numPage--;
			}
		}
		
		/**
		 * 
		 */
		public function set placePage(decay:int):void
		{
			trace(numPage);
			_decay = decay;
			if (numPage > 0) {
				addChild((a_Pages[numPage - 1] as PageManager));
				(a_Pages[numPage - 1] as PageManager).x = -480 + decay;
			}
			(a_Pages[numPage] as PageManager).x = decay;
			if (numPage < totalPage) {
				(a_Pages[numPage + 1] as PageManager).x = 480 + decay;
				addChild((a_Pages[numPage + 1] as PageManager));
			}
		}
		
		public function get placePage():int { return _decay; }
		
		public function handleResize(e:Event):void
		{
			trace("handleResize");
			placePage = 0;
		}
		
	}

}